package com.itheima.controller;

import com.itheima.pojo.PageBody;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping

    public Result findStudentByPage(@RequestParam(value = "name",defaultValue = "",required = false)String name,
                                    @RequestParam(value = "degree",required = false)Integer degree,
                                    @RequestParam(value = "clazzId",required = false)Integer clazzId,
                                    @RequestParam(value = "page",defaultValue = "1")Integer page,
                                    @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize){
        PageBody pageBody =  studentService.findStudentByPage(name,degree,clazzId,page,pageSize);
        return Result.success(pageBody);
    }
    @PostMapping
    public Result saveStudent(@RequestBody Student student){
        studentService.saveStudent(student);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result findStudentById(@PathVariable("id")Integer id){
        Student student = studentService.findStudentById(id);
        return Result.success(student);
    }
    @PutMapping
    public Result updateStudentBy(@RequestBody Student student){
        studentService.updateStudentById(student);
        return Result.success();
    }
    @DeleteMapping("/{ids}")
    public Result deleteStudentByIds(@PathVariable("ids")String ids){
        String[] split = ids.split(",");
        List<Integer> idsList = (List<Integer>) CollectionUtils.arrayToList(split);
        studentService.deleteStudentByIds(idsList);
        return Result.success();
    }
    @PutMapping("/violation/{id}/{score}")
    public Result updateStudentViolation(@PathVariable("id")Integer id,@PathVariable("score")Integer score){
        studentService.updateStudentViolation(id,score);
        return Result.success();
    }
}
